import axios from '@/js_sdk/xtshadow-axios/axios.min.js'
import store from '@/store/index.js'
import { PROXY_URL } from '@/constant/index.js'
import { toast } from '@/utils/tool.js'

const request = axios.create({
	baseURL: PROXY_URL,
	timeout: 10000,
	header: {
		// 'content-type': 'application/json'
	},
});
// 请求拦截器
request.interceptors.request.use(config => {
	const { token } = store.getters
	config.header.token = token
	return config
}, error => {
	return Promise.reject(error)
})

request.interceptors.response.use(response => {
	const { data, statusCode } = response
	if (statusCode !== 200) return Promise.reject(data)
	const { code, msg } = data
	if (code === 0) {
		if (msg === "请求参数缺token") {
			toast({ title: '请您先登录' }, () => {
				uni.redirectTo({
					url: '/pages/login/login'
				})
			})
			return Promise.reject(new Error('登录过期'))
		}
	}
	// if (code === 0) {
	// 	store.commit('logout')
	// 	toast({ title: '请您先登录' }, () => {
	// 		uni.redirectTo({
	// 			url: '/pages/login/login'
	// 		})
	// 	})
	// 	return Promise.reject(new Error('登录过期'))
	// }

	return data
}, error => {
	return Promise.reject(error)
})

export default function({ url, data, method = 'POST' }) {
	return request({ url, data, method })
}